package com.google.gwt.livinglife.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.livinglife.client.LivingLife;
import com.google.gwt.livinglife.client.data.ChallengeData;
import com.google.gwt.livinglife.client.data.ResponseData;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author amit
 * This widget would pick a random challenge from a given set of challenges 
 * and pick a random response for the challenge and show them every 5 seconds. 
 * The entire are is going to be clickable and the clicks would lead to a page
 *  showing the current challenge in its entirety (with all the responses and
 *  comments. Since multiple challenges will be flashed in the same space and
 *  there would be other widgets around this widget, it is a must that the size
 *  of this widget is fixed. Hence when we show challenges in this widget, we
 *  limit the number of character for the text of challenge and the response.
 */
public class FlashingChallengeWidget extends Composite implements ClickHandler {
	private int width_in_pixel = 700;
	private int height_in_pixel = 500;
	private int char_limit = 250;
	
	DecoratorPanel decorator = new DecoratorPanel();
	VerticalPanel root_panel = new VerticalPanel();
	InlineHTML line = new InlineHTML("<hr />");
	Anchor group_label = new Anchor();
	HTML response_label = new HTML("<center><h3>Response</h3></center>");
	BasicPostBox challengeBox = new BasicPostBox();
	BasicPostBox responseBox = new BasicPostBox();
	ChallengeData challenges[];
	int current_challenge_index;
	
	public FlashingChallengeWidget() {
		decorator.add(root_panel);
		root_panel.add(group_label);
		group_label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER); 
		root_panel.add(challengeBox);
		root_panel.add(line);
		root_panel.add(response_label);
		root_panel.add(responseBox);
		initWidget(decorator);
		Timer refreshTimer = new Timer() {
      @Override
      public void run() {
      	displayRandomChallenge();
      }
    };
    refreshTimer.scheduleRepeating(15000);
	}
	
	public void displayRandomChallenge() {
		if (challenges != null && challenges.length > 0) {
			current_challenge_index = Random.nextInt(challenges.length);
			displayChallenge(challenges[current_challenge_index]);
		}
	}
	private void displayChallenge(ChallengeData cd) {
		decorator.setPixelSize(width_in_pixel, height_in_pixel);
		if (cd.getGroup_name() == null) {
			cd.setGroup_name("");
		}
		group_label.setHTML("<center><h3>" + cd.getGroup_name() + " Challenge </h3></center>");
		group_label.setHref(LivingLife.getChallengeIdUrl(cd.getId()));
		int available_height = height_in_pixel - group_label.getOffsetHeight()
			- line.getOffsetHeight() - response_label.getOffsetHeight();
		int basic_post_height = (available_height / 2) - 5;
		if (basic_post_height < 100) {
			basic_post_height = 100;
		}
		challengeBox.setHeight(basic_post_height + "px");
		challengeBox.setWidth((width_in_pixel - 5) + "px");
		responseBox.setHeight(basic_post_height + "px");
		responseBox.setWidth((width_in_pixel - 5) + "px");
		
		challengeBox.draw_fixed_size(cd, basic_post_height, width_in_pixel, char_limit);
		if (cd.getResponses() != null && cd.getResponses().size() > 0) {
			int response_index = Random.nextInt(cd.getResponses().size());
			ResponseData rd = cd.getResponses().get(response_index);
			responseBox.draw_fixed_size(rd, basic_post_height, width_in_pixel, char_limit);
		} else {
			responseBox.setBlank();
		}
	}
	
	@Override
	public void onClick(ClickEvent event) {
		
	}

	public int getWidth_in_pixel() {
		return width_in_pixel;
	}

	public void setWidth_in_pixel(int widthInPixel) {
		width_in_pixel = widthInPixel;
	}

	public int getHeight_in_pixel() {
		return height_in_pixel;
	}

	public void setHeight_in_pixel(int heightInPixel) {
		height_in_pixel = heightInPixel;
	}

	public void setChallenges(ChallengeData[] challenges) {
		this.challenges = challenges;
	}
}
